﻿package  {
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import flash.external.ExternalInterface;
	
	/*
		This encapsulates the rotating shape and menu links.
		
		Attempting to abstract this into the following classes:
			
			1) ObjectRotate: this will handle all the abstract 3D rotations that are performed. Values will be sent from the
			spriteBoard class when respective mouse events are called.
			
			2) ObjectDraw: the class which draws the object to the screen after rotations are performed by ObjectRotate.
			
			3) ShapeModel: prototype of the faces and vertices of the shape to be drawn. Also to contain all properties.

	*/
	
	public class Object3D {
		
		private var shapeModel:ShapeModel;
		private var objRotate:ObjectRotate;
		private var objDraw:ObjectDraw;
		
		private var parentSpriteBoard:SpriteBoard;
		
		private var menuArray:MenuItemHandler;
		
		private var links:LinkLoader;
		
		public function xmlPathQuery():void {
			
		}
		
		//======
		public function Object3D(parentSpriteBoard:SpriteBoard) {
			
			//ExternalInterface.addCallback("filePathQuery", xmlPathQuery);
			
			this.parentSpriteBoard = parentSpriteBoard;
			
			shapeModel = new ShapeModel();
			// 
			links = new LinkLoader("http://markpalmer.dyndns.org/drupal/Test.xml",this); // XML reader
			
			menuArray = new MenuItemHandler(this, parentSpriteBoard);
			
			objRotate = new ObjectRotate(this,shapeModel);
			
			objDraw = new ObjectDraw(parentSpriteBoard, objRotate, shapeModel, menuArray);
			
			rotateAndDraw(0, 0, 0);
		}
		
		// editing...
		// this is called when xml link loader has parsed all elements
		public function createMenuItems()
		{
			var numberOfItems = links.getNumberOfItems();
			trace(numberOfItems);
			
			// hasn't yet been rotated?
			var vertices:Vector.<Vector3D> = shapeModel.getRotatedVertices();
			
			for (var i:int = 0; i < numberOfItems; i++)
			{
				var itemURL:String = links.getItemURLAtIndex(i);
				var itemDescription:String = links.getItemDescriptionAtIndex(i);
				
				var menuVertex:Point = new Point();

				menuVertex.x = vertices[i].x;
				menuVertex.y = vertices[i].y;
				
				menuArray.addNewItem(menuVertex,itemDescription,itemURL);
			}
		}
		
		public function rotateAndDraw(rX:Number, rY:Number, rZ:Number) {
			
			objRotate.rotate(rX, rY, rZ);
			objDraw.drawObject();
			
		}
		
		public function clearObjectDisplay() {
			objDraw.clearObjectDisplay();
		}
		
		public function showText():void {
			menuArray.showText();
		}
		
		public function hideText():void {
			menuArray.hideText();
		}
	}
	
}
